/**
 * @param {number[]} nums
 * @return {number}
 * 输入：nums = [100,4,200,1,3,2]
 * 输出：4
 * 解释：最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
 */
var longestConsecutive = function (nums) {
    const hashSet = new Set()
    nums.forEach(item => {
        hashSet.add(item)
    })
    const len = nums.length
    let maxLen = 0
    for (let i = 0; i < len; i++) {
        let k = 0
        let n = nums[i]
        while (hashSet.has(n)) {
            hashSet.delete(n)
            k++
            n--
        }
        n = nums[i] + 1
        while (hashSet.has(n)) {
            hashSet.delete(n)
            k++
            n++
        }
        maxLen = Math.max(maxLen, k)
    }
    return maxLen
};

console.log(longestConsecutive([100, 4, 200, 1, 3, 2]))